package org.ysh.demo.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript;


import java.io.File;
import java.io.IOException;

public class PdfUtils {

    public static void generatePdfContainJS(){

    }
    public static void main(String[] args) throws IOException {
//Creating PDF document object
        PDDocument document = new PDDocument();

//Creating a blank page
        PDPage blankPage = new PDPage();

//Adding the blank page to the document
        document.addPage(blankPage);

        String javaScript = "app.alert('欢迎关注cms.centyun.com');";

//Creating PDActionJavaScript object
        PDActionJavaScript PDAjavascript = new PDActionJavaScript(javaScript);

//Embedding java script
        document.getDocumentCatalog().setOpenAction(PDAjavascript);

        String imgPath = "D:\\TESTDATA\\data\\2222.png";

        for(int i=0;i<30;i++){

            PDImageXObject xObject = PDImageXObject.createFromFile(imgPath,document);
            PDPage page = new PDPage(PDRectangle.A4);
            PDPageContentStream stream = new PDPageContentStream(document,page);
            stream.drawImage(xObject,0,0);
            stream.close();
            document.addPage(page);
        }

//Saving the document
        document.save("D:\\centyun.pdf");
        System.out.println("PDF created");

//Closing the document
        document.close();
    }
}
